let express = require('express');
let userAction = require('../../action/userAction');
let router = express.Router();
const fs = require('fs');

router.post('/list', function (req, res) {
    userAction.list(req.body).then(res.res, res.rej);
});
router.post('/nrList', function (req, res) {
    userAction.no_register_userList(req.body).then(res.res, res.rej);
});
router.post('/create', function (req, res) {
    if (req.files)
        req.body.HeadImgUrl = req.files[0].imageUrl;
    req.body.CreatedAt = new Date();
    if(req.body.DeleteFileArr)
        deleteFile(JSON.parse(req.body.DeleteFileArr));
    userAction.create(req.body).then(res.res, res.rej);
});
router.post('/update', function (req, res) {
    if (req.files)
        req.body.HeadImgUrl = req.files[0].imageUrl;
    if(req.body.DeleteFileArr)
        deleteFile(JSON.parse(req.body.DeleteFileArr));
    userAction.update(req.body).then(res.res, res.rej);
});
router.post('/detail', function (req, res) {
    userAction.detail(req.body).then(res.res, res.rej);
});
router.post('/evaluate', function (req, res) {
    userAction.evaluate(req.body).then(res.res, res.rej);
});
module.exports = router;
function deleteFile(pathArr) {
    for(let i in pathArr){
        try {
            if( fs.existsSync(config.project_dir+"/public/"+pathArr[i]) ) {
                fs.unlink(config.project_dir+"/public/"+pathArr[i],function (err,result) {
                    console.log("err==",err);
                    console.log("result==",result);
                })
            }
        }catch (err){
            console.log("err==",err);
        }
    }

}
